package com.wd.jetpack.helper.binds

import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * 页面描述：normal bind class
 */
@BindingAdapter("enable")
fun setImgEnabled(view: ImageView, boolean: Boolean) {
    view.isEnabled = boolean
}

@BindingAdapter(value = ["imgRes"], requireAll = false)
fun bindImageRes(imageView: ImageView, res: Int?) {
    res?.let {
        imageView.setImageResource(res)
    }
}

@BindingAdapter("isBold")
fun setBold(view: TextView, isBold: Boolean) {
    if (isBold) {
        view.setTypeface(null, Typeface.BOLD)
    } else {
        view.setTypeface(null, Typeface.NORMAL)
    }
}

//绑定图片
@BindingAdapter(value = ["background"], requireAll = false)
fun bindBackground(v: View, color: Int) {
    v.background = ColorDrawable(color);
}

//View 可见性绑定
@BindingAdapter(value = ["visible"])
fun bindVisibility(v: View, visible: Boolean) {
    v.visibility = if (visible) View.VISIBLE else View.GONE
}

//RecyclerView adapter绑定
@BindingAdapter("linearAdapter")
fun bindAdapter(v: RecyclerView, adapter: RecyclerView.Adapter<*>?) {
    v.layoutManager = LinearLayoutManager(v.context)
    v.adapter = adapter
}

//View select绑定
@BindingAdapter(value = ["select"])
fun bindSelect(v: View, select: Boolean) {
    v.isSelected = select
}
